package com.activeMq.affair;

import org.apache.activemq.ActiveMQConnectionFactory;

import javax.jms.*;

public class Consumer {
    public static void main(String[] args) throws JMSException {
        ConnectionFactory factory = new ActiveMQConnectionFactory("tcp://127.0.0.1:61616");
        Connection connection = factory.createConnection();
        connection.start();
        Session session = connection.createSession(true, Session.CLIENT_ACKNOWLEDGE);
        Queue queue = session.createQueue("tx_queue_affair");
        MessageConsumer consumer = session.createConsumer(queue);
        try {
            for (int i = 0; i <10 ; i++) {
                TextMessage message = (TextMessage) consumer.receive();
                String text = message.getText();
                System.out.println("接收"+text);
                if (i==3)
                    throw new Exception("cx");
            }
            session.commit();
        }catch (Exception e){
            session.rollback();
        }


        consumer.close();
        session.close();
        connection.close();
    }
}
